package com.jacen.fzxs.controller;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jacen.fzxs.entity.TopEntity;
import com.jacen.fzxs.entity.WorkEntity;
import com.jacen.fzxs.service.ITopService;
import com.jacen.fzxs.service.IWorkService;
import com.jacen.fzxs.util.EntityUtils;
import com.jacen.fzxs.util.ServletUtils;
import com.jacen.fzxs.util.Utils;

@Controller
@RequestMapping("/web")
public class WebController extends BaseController {
	@Resource
	private ITopService topService;
	@Resource
	private IWorkService workService;
	
	//===============web outside interface=====================
	@RequestMapping("/init.do")
	@ResponseBody
	public String webInit(){
		TopEntity topEntity = topService.getTop();
		List<WorkEntity> workFlexs = workService.getWorkByPosition(WorkEntity.Position.FLEXS);
		List<WorkEntity> workItems = workService.getWorkByPosition(WorkEntity.Position.ITEM);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("top", topEntity);
		jsonObject.put("flexs", workFlexs);
		jsonObject.put("items", workItems);
		return JSON.toJSONString(jsonObject);
	}
	
	//=========================top manage start=======================================//
	@RequestMapping("/top/list.do")
	public String top(HttpServletRequest request,Integer curPage,ModelMap model) throws Exception{
			Map<String, String> params = ServletUtils.getParameterMap(request);
			List<TopEntity> list = topService.getPage(getPager(curPage, request),params);
			model.addAttribute("list", list);
			return "web/top/list";
	}
	@RequestMapping("/top/apply.do")
	@ResponseBody
	public String topApply(String id){
		try {
			topService.apply(id);
			return Utils.normalJsonResp();
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("项目应用异常");
		}
		
	}
	@RequestMapping(value="/top/insert.do",method=RequestMethod.GET)
	public String topInsert(){
		return "web/top/insert";
	}
	@RequestMapping(value="/top/insert.do",method=RequestMethod.POST)
	@ResponseBody
	public String topPostInsert(HttpServletRequest request,TopEntity entity){
		try {
			entity.setId(EntityUtils.createId32());
			entity.setStatus(TopEntity.Status.OFF);
			topService.insert(entity);
			return Utils.normalJsonResp();
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("新增异常");
		}
	}
	
	@RequestMapping(value="/top/update.do",method=RequestMethod.GET)
	public String topUpdate(ModelMap model,String id){
		TopEntity entity = topService.getById(id);
		model.addAttribute("entity", entity);
		return "web/top/update";
	}
	
	@RequestMapping(value="/top/update.do",method=RequestMethod.POST)
	@ResponseBody
	public String topPostUpdate(HttpServletRequest request,TopEntity entity){
		try {
			topService.update(entity);
			return Utils.normalJsonResp();
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("更新异常");
		}
	}
	
	@RequestMapping(value="/top/delete.do")
	@ResponseBody
	public String topDelete(HttpServletRequest request,String id){
		try {
			topService.deleteById(id);
			return Utils.normalJsonResp();
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("删除异常");
		}
	}
	
	//=========================top manage end=======================================//
	
	//=========================work manage start=====================================//
	@RequestMapping("/work/list.do")
	public String work(HttpServletRequest request,Integer curPage,ModelMap model) throws Exception{
		Map<String, String> params = ServletUtils.getParameterMap(request);
		List<WorkEntity> list = workService.getPage(getPager(curPage, request),params);
		model.addAttribute("list", list);
		return "web/work/list";
	}

	@RequestMapping(value="/work/insert.do",method=RequestMethod.GET)
	public String workInsert(){
		return "web/work/insert";
	}
	
	@RequestMapping(value="/work/insert.do",method=RequestMethod.POST)
	@ResponseBody
	public String workPostInsert(HttpServletRequest request,WorkEntity entity){
		try {
			entity.setId(EntityUtils.createId32());
			entity.setStatus(WorkEntity.Status.START);
			workService.insert(entity);
			return Utils.normalJsonResp();
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("新增异常");
		}
	}
	
	@RequestMapping(value="/work/update.do",method=RequestMethod.GET)
	public String workUpdate(ModelMap model,String id){
		WorkEntity entity = workService.getById(id);
		model.addAttribute("entity", entity);
		return "web/work/update";
	}
	
	@RequestMapping(value="/work/update.do",method=RequestMethod.POST)
	@ResponseBody
	public String workPostUpdate(HttpServletRequest request,WorkEntity entity){
		try {
			workService.update(entity);
			return Utils.normalJsonResp();
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("更新异常");
		}
	}
	
	@RequestMapping(value="/work/off.do",method=RequestMethod.POST)
	@ResponseBody
	public String workOff(HttpServletRequest request,String id){
		try {
			WorkEntity entity = workService.getById(id);
			entity.setStatus(WorkEntity.Status.OFF);
			workService.update(entity);
			return Utils.normalJsonResp();
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("禁用异常");
		}
	}
	
	@RequestMapping(value="/work/start.do",method=RequestMethod.POST)
	@ResponseBody
	public String workStart(HttpServletRequest request,String id){
		try {
			WorkEntity entity = workService.getById(id);
			entity.setStatus(WorkEntity.Status.START);
			workService.update(entity);
			return Utils.normalJsonResp();
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("启用异常");
		}
	}
	
	@RequestMapping(value="/work/delete.do")
	@ResponseBody
	public String workDelete(HttpServletRequest request,String id){
		try {
			workService.deleteById(id);
			return Utils.normalJsonResp();
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("删除异常");
		}
	}
//=========================work manage end=====================================//
	
	@RequestMapping("/service/list.do")
	public String service(ModelMap model){
		return "web/service/list";
	}
	@RequestMapping("/team/list.do")
	public String team(ModelMap model){
		return "web/team/list";
	}
	@RequestMapping("/about/list.do")
	public String about(ModelMap model){
		return "web/about/list";
	}
	@RequestMapping("/contact/list.do")
	public String contact(ModelMap model){
		return "web/contact/list";
	}
	
	@RequestMapping("/upload.do")
	public void upload(HttpServletRequest request,HttpServletResponse response,String model){
		MultipartHttpServletRequest multipartRequest =(MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		StringBuffer path = new StringBuffer(UploadController.UPLOAD_PATH + model);
		File uploadDir = new File(path.toString());
		if (!uploadDir.exists()) uploadDir.mkdir();
		String fileName = null;
		for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
			MultipartFile mf = entry.getValue();
			fileName = System.currentTimeMillis() + mf.getOriginalFilename();
			try{
 				File uploadFile = new File(path.toString()+File.separatorChar + fileName);
 				//拷贝文件
 				FileCopyUtils.copy(mf.getBytes(), uploadFile);
				ServletUtils.outputMsgInJson(response, 0, fileName);
 			}catch(Exception e){
 				e.printStackTrace();
 				ServletUtils.outputMsgInJson(response, 1, "文件上传异常");
 			}
			
		}
	}
}
